package com.Shop.Center.service;

import com.Shop.Center.dao.CreditMapper;
import com.Shop.Center.dao.EvaluateMapper;
import com.Shop.Center.dao.UserMapper;
import com.Shop.Center.entity.Credit;
import com.Shop.Center.entity.Evaluate;
import com.Shop.Center.entity.User;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EvaluateService {

    private static final double EVALUATECREDIT = 0.05;
    @Autowired
    EvaluateMapper evaluateMapper;
    @Autowired
    CreditMapper creditMapper;
    @Autowired
    UserMapper userMapper;
    public int add(long itemid,String content,long userid){
        Evaluate evaluate=new Evaluate();
        int id=new IdWorker().hashCode();
        evaluate.setContent(content);
        evaluate.setId(id);
        evaluate.setItemid(itemid);
        evaluate.setUserid(userid);
        if(content.length()>15){
            Credit credit = new Credit();
            credit.setPoint(EVALUATECREDIT);
            credit.setUserId(userid);
            credit.setOrderId((long) 0);
            creditMapper.insert(credit);
            User user = userMapper.selectById(userid);
            user.setPoint(user.getPoint()+EVALUATECREDIT);
            userMapper.updateById(user);
        }
        return evaluateMapper.insert(evaluate);
    }
}
